﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SolutionBLL;
using SolutionModel;
using SFast;
using SFast.Web;

public partial class admin_system_UserDetail : System.Web.UI.Page
{

    T_AdminUserBLL bll = new T_AdminUserBLL();
    protected String btn_text = "提 交";
    protected String rolesids = String.Empty;   //用户角色


    protected void Page_Load(object sender, EventArgs e)
    {
        String act = Request.QueryString["act"];  //url中的act
        String Action = Request.Form["action"];   //表单中的action

        #region 页面初始化
        //页面添加时初始化加载
        if (act == "add" && Action == null)
        {
            this.action.Value = "addSave";
            btn_text = "提  交";
            BindRole();
        }

        //页面编辑时初始化加载
        if (act == "edit" && Action == null)
        {
            String uid = String.Empty;
            try
            {
                uid = Request.QueryString["uid"];
            }
            catch (Exception emsg)
            {
                throw new Exception("参数有误，如无法解决请联系技术人员！详细错误信息：{0}", emsg);
            }
            btn_text = "保  存";
            this.action.Value = "editSave";
            BindRole();
        }

        //检查用户名是否已存在
        if (act == "checkingName" && Action == null)
        {
            String Account = Request["param"];
            int rs = bll.HasAdminUser(Account);
            if (rs > 0)
            {
                Response.Write("对不起，此用户不可用！");
            }
            else
            {
                Response.Write("y");
            }
            Response.End();
        }
        #endregion


        #region 按钮事件

        #region 添加页面“提交”按钮
        if (Action == "addSave")
        {
            try
            {
                T_AdminUser obj = Request._GetForm<T_AdminUser>();
                obj.UserID = Guid.NewGuid().ToString();
                obj.CreateTime = DateTime.Now;
                //添加前检查当前在添加的用户是否已经存在
                int re = bll.HasAdminUser(obj.Account);
                if (re > 0)
                {
                    Response.Write("{\"info\":\"对不起，用户名不可用，可能在数据中已经存在！\",\"status\":\"n\"}");
                    Response.End();
                }

                //添加用户
                bll.InsertAdminUser(obj, Request["rolesId"]);


                Response.Write("{\"info\":\"管理员添加成功！\",\"status\":\"y\"}");
            }
            catch (Exception emsg)
            {
                throw new Exception("{\"info\":\"管理员添加失败。详细错误信息：{0}\",\"status\":\"n\"}", emsg);
            }
            Response.End();
        }
        #endregion

        #region 编辑页面 “ 保存” 按钮事件
        if (Action == "editSave")
        {
            try
            {
                T_AdminUser adminusr = Request._GetForm<T_AdminUser>();
                bll.UpdateAdminUser(adminusr,Request.Form["rolesId"]);
                Response.Write("{\"info\":\"管理员编辑成功！\",\"status\":\"y\"}");
            }
            catch (Exception ex)
            {
                throw new Exception("{\"info\":\"管理员编辑失败。详细错误信息：{0}\",\"status\":\"n\"}", ex);
            }
            Response.End();
        }
        #endregion

        #endregion


    }

    public void BindRole()
    {
        T_UsersInRolesBLL rolesbll = new T_UsersInRolesBLL();
        var uid = Request.QueryString["uid"];
        if (!String.IsNullOrEmpty(uid))
        {
            List<T_AdminRole> list = rolesbll.GetRolesByUser(uid);
            List<String> listId = new List<string>();
            list.ForEach(o => listId.Add(o.RoleID));
            rolesids = listId._ToCSV(",");

            rp_rolesList.DataSource = list;
            rp_rolesList.DataBind();
        }
        
    }
}